Create a Rust program to find the GCD (Greatest Common Divisor) of two numbers.
Create a Rust program to find the GCD (Greatest Common Divisor) of two numbers.
344
20-Jun-2023
Aryan Kumar
24-Jun-2023Sure, here is a Rust program to find the GCD (Greatest Common Divisor) of two numbers:
Rust
This program first checks if one of the numbers is 0. If it is, then the program returns 0. Otherwise, the program loops while b is not 0. In each iteration of the loop, the program swaps a and b and then calculates the remainder of b divided by a. The program continues looping until b is 0. Finally, the program returns a.
To run this program, you can save it as a
.rsfile and then compile and run it using the following commands:Code snippet
This will print the following output:
Code snippet
This indicates that the GCD of 12 and 18 is 6.